package com.kingyun.gpsinspection.purificationservice.common.utils;

import com.cqhilink.api.common.utils.date.DateStyle;
import com.cqhilink.api.common.utils.date.DateUtil;

import java.text.DecimalFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * @Author 卢曼成
 * @Description 电子模版记录巡检率工具类
 * @CreateDateTime 2017-06-09 1:56
 **/
public class ETUtils {
    private static Calendar calendar;

    /**
     * @param day    天数
     * @param second 巡检次数
     * @param total  数据库查询次数
     * @return
     */
    public static String calculation(int day, int second, int total) {
        if (total == 0) {
            return null;
        }
        int num = day * second;
        if (num == total) {
            return "100%";
        }
        double number = 100 / (double) num;
        Double a = number * total;
        DecimalFormat df = new DecimalFormat("#.##");
        return df.format(a) + "%";
    }

    //判断闰年和平年求天数
    public static int isPingnian(int year) {
        if (((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0))
            return 366;
        else
            return 365;
    }

    //获取上周周一的日期
    public static int year() {
        calendar = Calendar.getInstance();
        return calendar.get(Calendar.YEAR);
    }

    //获取上周周一的日期
    public static String lastMonday(Date date) {
        calendar = Calendar.getInstance();
        calendar.setTime(date);
        int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK) - 1;
        int offset = 1 - dayOfWeek;
        calendar.add(Calendar.DATE, offset - 7);
        return DateUtil.DateToString(calendar.getTime(), DateStyle.YYYY_MM_DD);
    }

    //获取上周周日的日期
    public static String lastSunday(Date date) {
        calendar = Calendar.getInstance();
        calendar.setTime(date);
        int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK) - 1;
        int offset = 7 - dayOfWeek;
        calendar.add(Calendar.DATE, offset - 7);
        return DateUtil.DateToString(calendar.getTime(), DateStyle.YYYY_MM_DD);
    }

    ////获取前月的第一天
    public static String firstMonth(Date date) {
        calendar = Calendar.getInstance();//获取当前日期
        calendar.setTime(date);
        calendar.add(Calendar.MONTH, -1);
        calendar.set(Calendar.DAY_OF_MONTH, 1);//设置为1号,当前日期既为本月第一天
        return DateUtil.DateToString(calendar.getTime(), DateStyle.YYYY_MM_DD);
    }

    /**
     * 根据月份获取某月最后一天
     *
     * @param date 日期
     * @param num  最后一天加1，如果上个月是31号，获得日期将是下个月的1号，默认0
     * @return
     */
    public static String bottomMonth(Date date, int num) {
        calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.DAY_OF_MONTH, num);
        return DateUtil.DateToString(calendar.getTime(), DateStyle.YYYY_MM_DD);
    }


    /**
     * 获取当前时间的前一天日期
     *
     * @param date
     * @return
     */
    public static String getFrontDate(Date date, int num) {
        calendar = Calendar.getInstance(); //得到日历
        calendar.setTime(date);//把当前时间赋给日历
        calendar.add(Calendar.DAY_OF_MONTH, num);  //设置为前一天
        return DateUtil.DateToString(calendar.getTime(), DateStyle.YYYY_MM_DD);
    }

    /**
     * 获取当前时间的后一天日期
     *
     * @param date
     * @return
     */
    public static String getAfterDate(Date date) {
        calendar = Calendar.getInstance(); //得到日历
        calendar.setTime(date);//把当前时间赋给日历
        calendar.add(Calendar.DAY_OF_MONTH, 1);  //设置为后一天
        return DateUtil.DateToString(calendar.getTime(), DateStyle.YYYY_MM_DD);
    }


    /**
     * 获取某年第一天日期
     *
     * @param year 年份
     * @return Date
     */
    public static String getYearFirst(int year) {
        calendar = Calendar.getInstance();
        calendar.clear();
        calendar.set(Calendar.YEAR, year);
        return DateUtil.DateToString(calendar.getTime(), DateStyle.YYYY_MM_DD);
    }

    /**
     * 获取某年最后一天日期
     *
     * @param year 年份
     * @return Date
     */
    public static String getYearLast(int year) {
        calendar = Calendar.getInstance();
        calendar.clear();
        calendar.set(Calendar.YEAR, year);
        calendar.roll(Calendar.DAY_OF_YEAR, -1);
        return DateUtil.DateToString(calendar.getTime(), DateStyle.YYYY_MM_DD);
    }

    /**
     * 获取某年第一天日期
     *
     * @param year 年份
     * @return Date
     */
    public static Date getCurrYearFirst(int year) {
        calendar.clear();
        calendar.set(Calendar.YEAR, year);
        Date currYearFirst = calendar.getTime();
        return calendar.getTime();
    }

    /**
     * 获取某年最后一天日期
     *
     * @param year 年份
     * @return Date
     */
    public static Date getCurrYearLast(int year) {
        Calendar calendar = Calendar.getInstance();
        calendar.clear();
        calendar.set(Calendar.YEAR, year);
        calendar.roll(Calendar.DAY_OF_YEAR, -1);
        return calendar.getTime();
    }

    /**
     * 得到某月最后一天
     *
     * @param num 某月
     * @return
     */
    public static int getMonthMaxDay(int num) {
        calendar = Calendar.getInstance();
        //调到上个月
        calendar.add(Calendar.MONTH, num);
        //得到一个月最最后一天日期(31/30/29/28)
        return calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
    }

    /**
     * 输入yyyy-MM-dd 日期获取剩余天数
     *
     * @param startDate 开始日期
     * @param endDate   结束日期
     * @return int
     */
    public static int getYearSize(String startDate, String endDate) {
        calendar = Calendar.getInstance();
        Calendar calendar2 = Calendar.getInstance();
        //设置日期
        calendar.setTime(DateUtil.StringToDate(startDate, DateStyle.YYYY_MM_DD));//开始日期
        calendar2.setTime(DateUtil.StringToDate(endDate, DateStyle.YYYY_MM_DD));//结束日期
        //设置时间为0时
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar2.set(Calendar.HOUR_OF_DAY, 0);
        calendar2.set(Calendar.MINUTE, 0);
        calendar2.set(Calendar.SECOND, 0);
        //得到两个日期相差的天数
        int days = (((int) (calendar2.getTime().getTime() / 1000) - (int) (calendar.getTime().getTime() / 1000)) / 3600 / 24) + 1;
        return days;
    }

    /**
     * 输入yyyy-MM-dd 日期获取剩余天数
     *
     * @param startDate 开始日期
     * @param endDate   结束日期
     * @return int
     */
    public static int getYearSize(Date startDate, Date endDate) {
        calendar = Calendar.getInstance();
        Calendar calendar2 = Calendar.getInstance();
        //设置日期
        calendar.setTime(startDate); //开始日期
        calendar2.setTime(endDate); //结束日期
        //设置时间为0时
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar2.set(Calendar.HOUR_OF_DAY, 0);
        calendar2.set(Calendar.MINUTE, 0);
        calendar2.set(Calendar.SECOND, 0);
        //得到两个日期相差的天数
        int days = ((int) (calendar2.getTime().getTime() / 1000) - (int) (calendar.getTime().getTime() / 1000)) / 3600 / 24;
        return days;
    }

    /**
     * 获取某年某月天数
     *
     * @param str
     * @return
     */
    public static int getDaySize(String str) {
        calendar = Calendar.getInstance();
        calendar.setTime(DateUtil.StringToDate(str, DateStyle.YYYY_MM_DD));
        calendar.set(Calendar.DATE, 1);
        calendar.roll(Calendar.DATE, -1);
        return calendar.get(Calendar.DATE);
    }

}
